﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Reflection;
using System.Data.Common;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Json;
using UtilPublic.ServerLoger;
using UtilLib;
using Newtonsoft.Json;

namespace UtilPublic.Serialization
{
    /// <summary>
    /// Json类
    /// </summary>
    public static partial class Jsh
    {
        #region JsonConvert
        /// <summary>
        /// 序列化单个对象
        /// </summary>
        public static string Jstr<T>(this T obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        /// <summary> 
        /// 反序列化单个对象
        /// </summary> 
        public static T Jobj<T>(this string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }

        /// <summary>
        /// 反序列化单个对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool J2O<T>(string json, out T obj) where T : new()
        {
            if (!json.IsNullOrEmpty())
            {
                try
                {
                    obj = Jobj<T>(json);
                    return true;
                }
                catch { }
            }
            obj = new T();
            return false;
        }

        /// <summary>
        /// 打印单个对象
        /// </summary>
        public static string Jdump<T>(this T obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        #endregion



    }
}